/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.sentry.config;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * PropertiesProvider
 *
 * @since 2021-06-02
 */
public interface PropertiesProvider {
    /**
     * Resolves property given by it's name.
     *
     * @param property - the property name
     * @return property value or {@code null} if not found.
     */
    @Nullable
    String getProperty(@NotNull String property);

    /**
     * Resolves a map for a property given by it's name.
     *
     * @param property - the property name
     * @return the map or empty map if not found
     */
    @NotNull
    Map<String, String> getMap(@NotNull String property);

    /**
     * Resolves a list of values for a property given by it's name.
     *
     * @param property - the property name
     * @return the list or empty list if not found
     */
    @NotNull
    default List<String> getList(@NotNull String property) {
        final String value = getProperty(property);
        return value != null ? Arrays.asList(value.split(",")) : Collections.emptyList();
    }

    /**
     * Resolves property given by it's name.
     *
     * @param property - the property name
     * @param defaultValue - the default value if property is not set
     * @return property value or the default value if not found.
     */
    @NotNull
    default String getProperty(@NotNull String property, @NotNull String defaultValue) {
        final String result = getProperty(property);
        return result != null ? result : defaultValue;
    }

    /**
     * Resolves a boolean property given by it's name.
     *
     * @param property - the property name
     * @return property value or the default value if not found.
     */
    @Nullable
    default Boolean getBooleanProperty(@NotNull String property) {
        final String result = getProperty(property);
        return result != null ? Boolean.valueOf(result) : null;
    }

    /**
     * Resolves a {@link Double} property given by it's name.
     *
     * @param property - the property name
     * @return property value or the default value if not found.
     */
    @Nullable
    default Double getDoubleProperty(@NotNull String property) {
        final String result = getProperty(property);
        return result != null ? Double.valueOf(result) : null;
    }
}
